package com.example.demo1.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo1.common.Result;
import com.example.demo1.entity.Case;
import com.example.demo1.service.CaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@ResponseBody
@RequestMapping("/api/testcase")
public class CaseController {

    @Autowired
    CaseService caseService;
    @PostMapping("/add")
    public Result addCase(@RequestBody Case testCase){
        try{
            caseService.addCase(testCase);
            return Result.success();
        }catch (Exception e){
            return Result.fail(e.toString());
        }
    }

    @PostMapping("/update")
    public Result updateCase(@RequestBody Case testCase){
        try{
            caseService.updateCase(testCase);
            return Result.success();
        }catch (Exception e){
            return Result.fail(e.toString());
        }
    }

    @PostMapping("/delete")
    public Result deleteCase(@RequestBody Case testCase){
        try {
            caseService.deleteCase(testCase);
            return Result.success();
        }catch (Exception e){
            return Result.fail(e.toString());
        }
    }


    //    @PathVariable获取路径上的参数，@RequestBody获取请求体
    @PostMapping("/list/{currentPage}/{pageSize}")
    public Result getProject(@PathVariable int currentPage,
                             @PathVariable int pageSize,
                             @RequestBody Case testCase){
        IPage<Case> IPageCase=caseService.getCaseList(currentPage, pageSize, testCase);
        return Result.success(IPageCase);
    }
    @GetMapping("/list/all")
    public Result getCaseAll() {
        return Result.success(caseService.getCaseAll());
    }


}
